<?php

namespace Common\Model;

use Think\Model;
use Yansongda\Pay\Pay;

class OrdersModel extends Model
{
    public function orderPayment($order)
    {
        if(!$order['buyer_id'] || !in_array($order['payment'],['wechat','alipay']) || !$order['item_id']){
            return ['error'=>1,'msg'=>'参数错误'];
        }

        //获取商品信息
        $itemsModel = D('Items');
        $items = $itemsModel->find($order['item_id']);

        $order['item_amount']   = $items['price'] * $order['item_num'];
        $order['order_amount']  = $items['coupon_price'] * $order['item_num'];

        //生成订单
        $result = $this->add($order);
        
        //生成支付数据
        $order['order_id']  = $result;
        $order['item_name'] = $items['title'];
        if($payment = $this->getPayment($order)){
            return ['error'=>0,'data'=>$payment];
        }

        return ['error'=>1,'msg'=>'支付参数错误'];

    }

    public function getPayment($order)
    {
        //返回相应的支付参数
        $doDriver = 'createPayOrderBy' . ucfirst($order['payment']);
        return $this->$doDriver('app', $order);
    }

    /**
     * 创建支付宝支付单
     * @param $gateway
     * @param $order
     * @return bool|mixed
     */
    public function createPayOrderByAlipay($gateway, $order)
    {
        $config = [];
        //订单数据
        $configBiz = [
            'out_trade_no' => $order['order_sn'],
            'total_amount' => $order['order_amount'],
            'subject' => $order['item_name'] . ' X ' . $order['item_num'],
        ];
        switch ($gateway) {
            case 'app' :
                $config = [
                    'alipay' => [
                        'app_id' => 'pay.alipay.app_id',
                        'notify_url' => 'pay.alipay.notify_url' . $gateway,
                        //'return_url' => config('pay.alipay.return_url') . '?oid='.$order['order_id'],
                        'ali_public_key' => 'pay.alipay.ali_public_key',
                        'private_key' => 'pay.alipay.private_key',
                    ]
                ];
                break;
            case 'wap':
                $config = [
                    'alipay' => [
                        'app_id' => 'pay.alipay.app_id',
                        'notify_url' => 'pay.alipay.notify_url' . $gateway,
                        'return_url' => 'pay.alipay.return_url' . '?oid='.$order['order_id'],
                        'ali_public_key' => 'pay.alipay.ali_public_key',
                        'private_key' => 'pay.alipay.private_key',
                    ]
                ];
                break;
        }
        $pay = new Pay($config);
        if ($result = $pay->driver('alipay')->gateway($gateway)->pay($configBiz)) {
            return $result;
        }
        return false;
    }

    /**
     * 创建微信支付单
     * @param $gateway
     * @param $order
     * @return bool|mixed
     */
    public function createPayOrderByWechat($gateway, $order)
    {
        $config = [];
        $configBiz['out_trade_no'] = $order['order_sn'];
        $configBiz['total_fee'] = $order['order_amount'] * 100; // 订单金额，**单位：分**
        $configBiz['body'] = $order['item_name'] . ' X ' . $order['item_num'];
        $configBiz['spbill_create_ip'] = getClientIp();
        switch ($gateway) {
            case 'app' :
                $config['wechat']['appid'] = 'pay.wechat.app.app_id';
                $config['wechat']['mch_id'] = 'pay.wechat.app.mch_id';
                $config['wechat']['notify_url'] = 'pay.wechat.notify_url' . $gateway;
                $config['wechat']['key'] = 'pay.wechat.app.key';
                break;
            case 'wap':
                $config['wechat']['app_id'] = 'pay.wechat.app.app_id';
                $config['wechat']['mch_id'] = 'pay.wechat.app.mch_id';
                $config['wechat']['notify_url'] = 'pay.wechat.notify_url'.$gateway;
                $config['wechat']['return_url'] = 'pay.wechat.return_url' . '?oid='.$order['order_id'];
                $config['wechat']['key'] = 'pay.wechat.app.key';
                break;
            default:
                return false;
        }
        $pay = new Pay($config);
        if ($result = $pay->driver('wechat')->gateway($gateway)->pay($configBiz)) {
            return $result;
        }
        return false;
    }
}